package com.example.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.FragmentTransaction;

import com.example.home.fragment.CartFragment;
import com.example.home.fragment.ClassifyFragment;
import com.example.home.fragment.HomeFragment;
import com.example.home.fragment.MineFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    HomeFragment homeFragment;
    ClassifyFragment classifyFragment;
    CartFragment cartFragment;
    MineFragment mineFragment;
    BottomNavigationView bottomNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        selectItem(0);

        bottomNavigationView = findViewById(R.id.menu_bar);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if(item.getItemId() == R.id.menu_home){
                    selectItem(0);
                    return true;
                }
                if(item.getItemId() == R.id.menu_classify){
                    selectItem(1);
                    return true;
                }
                if(item.getItemId() == R.id.menu_cart){
                    selectItem(2);
                    return true;
                }
                if(item.getItemId() == R.id.menu_mine){
                    selectItem(3);
                    return true;
                }
                return false;
            }
        });

    }

    public void selectItem(int index) {
        FragmentTransaction ft =getSupportFragmentManager().beginTransaction();
        hideFragment(ft);
        if(index == 0){
            if(homeFragment == null){
                homeFragment = new HomeFragment();
                ft.add(R.id.main_content,homeFragment);
            }else {
                ft.show(homeFragment);
            }
        }
        if(index == 1){
            if(classifyFragment == null){
                classifyFragment = new ClassifyFragment();
                ft.add(R.id.main_content,classifyFragment);
            }else {
                ft.show(classifyFragment);
            }
        }
        if(index == 2){
            if(cartFragment == null){
                cartFragment = new CartFragment();
                ft.add(R.id.main_content,cartFragment);
            }else {
                ft.show(cartFragment);
            }
        }
        if(index == 3){
            if(mineFragment == null){
                mineFragment = new MineFragment();
                ft.add(R.id.main_content,mineFragment);
            }else {
                ft.show(mineFragment);
            }
        }
        ft.commit();
    }

    public void hideFragment(FragmentTransaction ft) {
        if(homeFragment!=null){
            ft.hide(homeFragment);
        }
        if(classifyFragment!=null){
            ft.hide(classifyFragment);
        }
        if(cartFragment!=null){
            ft.hide(cartFragment);
        }
        if(mineFragment!=null){
            ft.hide(mineFragment);
        }
    }
}
